<?php
/**
 * Created by xun at 2020/10/14
 */

namespace backend\models\c2\entity\rest;


use common\components\behaviors\AttachmentBehavior;
use common\models\c2\statics\EntityModelStatus;
use common\models\c2\statics\FromInputType;
use common\models\c2\statics\ImageSize;
use Yii;
use yii\helpers\ArrayHelper;

class BeUserProfile extends \backend\models\c2\entity\BeUserProfile
{

    use \common\traits\AttachmentTrait;

    public function rules()
    {
        return ArrayHelper::merge(parent::rules(), [
            [['name', 'public_email', 'gravatar_email', 'location', 'website', 'bio', 'timezone', 'firstname', 'lastname', 'avatar',
                'gravatar_id', 'gravatar_email'], 'string', 'max' => 255],
            [['user_id', 'position', 'status'], 'integer'],
            [['created_at', 'updated_at', 'avatar', 'birthday'], 'safe'],
        ]); // TODO: Change the autogenerated stub
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'attachmentsBehavior' => [
                'class' => AttachmentBehavior::className(),
                'attributesDefinition' => [
                    'avatar' => [
                        'class' => \common\models\c2\entity\EntityAttachmentImage::className(),
                        'validator' => 'image',
                        'rules' => [
                            'maxFiles' => 1,
                            'extensions' => Yii::$app->params['config']['upload']['imageWhiteExts'],
                            // 'extensions' => ['jpg'],
                            'maxSize' => Yii::$app->params['config']['upload']['maxFileSize'],
                            // 'maxSize' => '2048',
                        ],
                    ],
                ],
            ],
        ]);
    }

    public function loadDefaultValues($skipIfSet = true)
    {
        return parent::loadDefaultValues($skipIfSet); // TODO: Change the autogenerated stub
    }

    public function buildInputItems()
    {
        $inputItems = [];
        $labels = $this->attributeLabels();

        $inputItems[] = ['name' => 'avatar', 'label' => $labels['avatar'], 'value' => $this->getOriginalUrl(), 'component' => FromInputType::XFILEINPUT, 'accept' => 'image/*', 'placeholder' => '', 'errors' => ''];
        // $inputItems[] = ['name' => 'user_id', 'label' => $labels['user_id'], 'component' => FromInputType::INPUT, 'value' => $this->user_id, 'placeholder' => '', 'errors' => ''];
        $inputItems[] = ['name' => 'name', 'label' => $labels['name'], 'component' => FromInputType::INPUT, 'value' => $this->name, 'placeholder' => '', 'errors' => ''];
        $inputItems[] = ['name' => 'public_email', 'label' => $labels['public_email'], 'component' => FromInputType::INPUT, 'value' => $this->public_email, 'placeholder' => '', 'errors' => ''];
        $inputItems[] = ['name' => 'location', 'label' => $labels['location'], 'component' => FromInputType::INPUT, 'value' => $this->location, 'placeholder' => '', 'errors' => ''];
        $inputItems[] = ['name' => 'website', 'label' => $labels['website'], 'component' => FromInputType::INPUT, 'value' => $this->website, 'placeholder' => '', 'errors' => ''];
        $inputItems[] = ['name' => 'bio', 'label' => $labels['bio'], 'component' => FromInputType::INPUT, 'value' => $this->bio, 'placeholder' => '', 'errors' => ''];
        $inputItems[] = ['name' => 'gravatar_email', 'label' => $labels['gravatar_email'], 'component' => FromInputType::INPUT, 'value' => $this->gravatar_email, 'placeholder' => '', 'errors' => ''];
        $inputItems[] = ['name' => 'gravatar_id', 'label' => $labels['gravatar_id'], 'component' => FromInputType::INPUT, 'value' => $this->gravatar_id, 'placeholder' => '', 'errors' => ''];
        $inputItems[] = ['name' => 'timezone', 'label' => $labels['timezone'], 'component' => FromInputType::INPUT, 'value' => $this->timezone, 'placeholder' => '', 'errors' => ''];
        $inputItems[] = ['name' => 'firstname', 'label' => $labels['firstname'], 'component' => FromInputType::INPUT, 'value' => $this->firstname, 'placeholder' => '', 'errors' => ''];
        $inputItems[] = ['name' => 'lastname', 'label' => $labels['lastname'], 'component' => FromInputType::INPUT, 'value' => $this->lastname, 'placeholder' => '', 'errors' => ''];
        $inputItems[] = ['name' => 'birthday', 'label' => $labels['birthday'], 'component' => FromInputType::INPUT, 'value' => $this->birthday, 'placeholder' => '', 'errors' => ''];
        $inputItems[] = ['name' => 'position', 'label' => $labels['position'], 'component' => FromInputType::INPUT, 'inputType' => 'number', 'value' => $this->position, 'placeholder' => '', 'errors' => ''];
        $inputItems[] = ['name' => 'status', 'label' => $labels['status'], 'component' => FromInputType::CHECKBOX, 'status' => EntityModelStatus::getLabel($this->status), 'value' => $this->status, 'placeholder' => '', 'errors' => ''];

        // $safes = ['created_at', 'id',];
        // foreach ($this->fields() as $field) {
        //     if (!in_array($field, $safes)) {
        //         $inputItems[] = [
        //             'name' => $field,
        //             'label' => $labels[$field],
        //             'inputType' => 'text',
        //             'component' => FromInputType::getComponent(FromInputType::INPUT),
        //             'value' => $this->$field,
        //             'placeholder' => '',
        //             'errors' => '',
        //         ];
        //     }
        // }
        return $inputItems;
    }
}